<template>
    <div class='div'>
        <global-tables
            :total="total"
            :list="list"
            :loading="loading"
            :columns="columns"
            :current="search.pageindex"
            @changePage="handleChangePage"
            @handleAllSelect="handleAllSelect"
           
        >
            <Form :model="search" :label-width="80"><div class="table-top-wrap"></div></Form>
        </global-tables>
        <div>
            <Modal :mask-closable="false" v-model="isModal" :title="ModalTitle">
                <div v-if="isModal">
                    <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="100">
                        <Row>
                               <i-col :lg="{ span: 15}">
                                <FormItem label="参数" prop="Param">
                                    <i-input v-model="formValidate.Param" placeholder="请输入参数"></i-input>
                                </FormItem>
                            </i-col> 
                            <div class='form'> 
                             <i-col :lg="{ span: 15}">
                                <FormItem label="描述" prop="Remark">
                                    <i-input type="textarea" v-model="formValidate.Remark" placeholder="请输入描述" ></i-input>
                                </FormItem>
                            </i-col>
                            </div>
                        </Row>
                    </Form>
                </div>
                <div slot="footer">
                    <Button size="large" @click="handleClose">取消</Button>
                    <Button type="primary" size="large" @click="handleSubmit('formValidate')">确定</Button>
                </div>
            </Modal>
        </div>
    </div>
</template>

<script>
/**
 * 全局参数列表
 */
import { cyclicRouting } from '@/util';
import { getModulePermission } from '@/api/user';
import { getTablesList,postSubmitForm } from './service'
import GlobalTables from '@/components/GlobalTables';
export default {
    name: 'SystemParam',
    
    components: {
        GlobalTables
    },
    data() {
        return {
            isModal: false,
            loading: true,
            total: 0,
            ModalTitle: '修改全局参数',
            isEdit: false,
            allSelectArr: null,
            search: {
                keyword: '',
                pageindex: 1,
                pagesize: 10
            },
            itemId: null,
            formValidate: this.initFormValidate(),
            ruleValidate: this.initRuleValidate(),
            columns: [
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },
                {
                    title: '标题',
                    key: 'Title'
                },
                {
                    title: '参数',
                    key: 'Param',
                },
                {
                    title: '描述',
                    key: 'Remark',
                },
                            {
                    title: '操作',
                    width: 200,
                    align: 'center',
                    render: (h, params) => {
                        const { isEdit } = this;
                        return h('div', [
                            isEdit &&
                                h(
                                    'Button',
                                    {
                                        props: {
                                            type: 'primary',
                                            size: 'small'
                                        },
                                        style: {
                                            background: '#5ab1ef',
                                            border: 'none',
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                this.handleBtnClick(params.row, 'edit');
                                            }
                                        }
                                    },
                                    '修改'
                                ),
                        ]);
                    }
                }
            ],
            list: []
        };
    },
    mounted() {
        /* 获取菜单的id */
        cyclicRouting(JSON.parse(sessionStorage.getItem('user')).menuList, this.$route, id => {
            /* 按钮权限请求 */
            getModulePermission({
                roleid: JSON.parse(sessionStorage.getItem('user')).roleid,
                moduleid: id
            }).then(res => {
                /* 按钮权限赋值 */
                for (let item in res.data) {
                     if (res.data[item].name == 'edit') {
                        this.isEdit = true;
                    }
                }
                /* 列表请求 */
                this.getTablesListData();
            });
        });
    },
    methods: {
        /* 表格数据请求 */
        getTablesListData() {
            const data = this.search;
            getTablesList(data).then(res => {
                this.list = res.data;
                this.loading = false;
                this.total = res.data.records;
            });
        },
        /* 表格分页方法 */
        handleChangePage(type, page) {
            this.loading = true;
            if (type == 'page') {
                this.search.pageindex = page;
                this.getTablesListData();
            } else {
                this.search.pagesize = page;
                this.getTablesListData();
            }
        },
        /* 表格查询 */
        handleSearch() {
            this.loading = true;
            this.getTablesListData();
        },
        /* 表格清空 */
        handleEmpty() {
            this.search.keyword = '';
            this.getTablesListData();
        },
        /* 关闭对话框 */
        handleClose() {
            this.isModal = false;
        },
        /* 复选框选中 */
        handleAllSelect(list) {
            let arr = [];
            for (let item in list) {
                arr.push(list[item].ID);
                arr.join(',');
            }
            arr = arr.join(',');
            this.allSelectArr = arr;
        },
        /* 对话框提交 */
        handleSubmit(name) {
          const { formValidate} = this;
            this.$refs[name].validate(valid => {
                if (valid) {
                    let data = [{
                        ID:this.itemId,
                        Param: formValidate.Param,
                        Remark: formValidate.Remark,
                    }];
                    postSubmitForm(data).then(res => {
                        if (res.state == 'success') {
                            this.$Message.success('操作成功');
                            this.loading = true;
                            this.handleClose(); 
                            this.formValidate = this.initFormValidate()
                            this.getTablesListData();
                        } else {
                            this.$Message.error(res.message);
                        }
                    });
                }
            });
        },
        /* 对话框操作点击 */
        handleBtnClick(item, type) {
           this.initFormValidate();
            if (type == 'edit') {
                this.isModal = true;
                if (type == 'edit') {
                    this.type = 'edit';
                    this.itemId = item.ID;
                    this.ModalTitle = '修改全局参数';
                }
                this.formValidate = {
                            ...this.formValidate,
                             Param:item.Param,
                            Remark: item.Remark
                        };
             }
        },
        /* 表单数据 */
        initFormValidate() {
            return {
                Param:'' ,
                Remark:'',
            };
            
        },
        /* 表单校验 */
        initRuleValidate() {
            return {
                Param: [{ required: true, message: '请输入参数数字', trigger: 'change' /*pattern: '验证数字：^[0-9]*$'*/ }],
                 Remark: [{ required: true, message: '请输入描述内容', trigger: 'change' }],
            };
        }
    }
};
</script>

<style lang="less" scoped>
.form-item-width {
    width: 300px;
}
.btn-wrap {
    margin: 0 15px;
}
.dle-btn {
    margin: 0 10px;
}
.search-con {
    padding: 10px 0;
    .search {
        &-col {
            display: inline-block;
            width: 200px;
        }
        &-input {
            display: inline-block;
            width: 200px;
            margin-left: 2px;
        }
        &-btn {
            margin-left: 2px;
        }
    }
}

</style>
<style scoped>
.div >>>.table-top-wrap{
    padding-bottom: 0px;
}
.form >>>.ivu-input-wrapper{
    width:165%;
} 
.form >>>.ivu-input{
    min-height: 100px;
} 
</style>






